package cn.zhiran.basic.service.impl;

import cn.zhiran.basic.mapper.BaseMapper;
import cn.zhiran.basic.query.BaseQuery;
import cn.zhiran.basic.service.IBaseService;
import cn.zhiran.basic.util.PageList;
import cn.zhiran.org.domain.Department;
import cn.zhiran.org.query.DepartmentQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T>{
    @Autowired
    private BaseMapper<T> mapper;

    @Override
    public List<T> loadAll() {

        return mapper.loadAll();
    }

    @Override
    public T loadById(Long id) {

        return mapper.loadById(id);
    }

    @Transactional
    @Override
    public void delete(Long id) {

        mapper.delete(id);
    }

    @Transactional
    @Override
    public void update(T t) {
        mapper.update(t);
    }

    @Transactional
    @Override
    public void insert(T t) {
        mapper.insert(t);
    }


    @Override
        public PageList<T> queryData(BaseQuery query) {
        //总条数
        Integer totals = mapper.queryCount(query);
        if(totals==0){
            return  new PageList<T>();
        }
        //每页显示条数
        List<T> rows = mapper.queryData(query);
        return new PageList<T>(totals,rows);
    }

    @Override
    @Transactional
    public void patchDelete( List<Long> ids) {
        mapper.batchDelete(ids);
    }

}
